package ruifight.member.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ruifight.member.model.UserInfo;
import ruifight.member.model.security.MyUserDetail;
import ruifight.member.service.UserInfoService;
import ruifight.member.util.MD5Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/interface")
public class UserInfoController {

	private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Autowired
	public UserInfoService userInfoService;

	/**
	 * 获取用户信息列表
	 * @return
	 */
	@RequestMapping("/getUserInfoList")
	@ResponseBody
	public Map<String, Object> getUserInfoList(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		String cur_page= paramMap.get("cur_page").toString();
		String page_size= paramMap.get("page_size").toString();
		PageInfo<Map<String, Object>> pageInfo = userInfoService.getUserInfoList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;
	}
	/**
	 * 获取登录用户信息
	 * @return
	 */
	@RequestMapping("/getLoginUserInfo")
	@ResponseBody
	public UserInfo getLoginUserInfo() {
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserInfo userInfo=userInfoService.selectByPrimaryKey(ud.getUserInfoId());
		return userInfo;
	}
	/**
	 * 修改密码
	 * @return
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Map<String, Object> updatePassword(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			String oldPassword= paramMap.get("oldPassword").toString();
			String password= paramMap.get("password").toString();
			if(!ud.getPwd().equals(MD5Util.MD5(oldPassword))){
				map.put("state",0);
				map.put("msg","修改失败，原密码不正确");
				return map;
			}
			UserInfo userInfo=new UserInfo();
			userInfo.setUserInfoId(ud.getUserInfoId());
			userInfo.setPassword(MD5Util.MD5(password));
			userInfoService.updateByPrimaryKey(userInfo);
			map.put("state",1);
			map.put("msg","修改成功");
			return map;
		}catch(Exception e){
			logger.error("修改密码失败",e);
		}
		map.put("state",0);
		map.put("msg","修改失败");
		return map;
	}

	/**
	 * 重置密码
	 * @return
	 */
	@RequestMapping("/resetPassword")
	@ResponseBody
	public Map<String, Object> resetPassword(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		String userInfoId = paramMap.get("userInfoId").toString();
		try{
			UserInfo userInfo = userInfoService.selectByPrimaryKey(Long.parseLong(userInfoId));
			userInfo.setPassword(MD5Util.MD5(userInfo.getCertificateCode().substring(12,18)));
			userInfoService.updateByPrimaryKeySelective(userInfo);
			map.put("state",1);
			map.put("msg","重置成功");
		} catch (Exception e){
			map.put("state",0);
			map.put("msg","重置失败");
		}
		return map;
	}
}
